home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 65.zip / BS1 part 65 / Math Visin v2.1 disk 1.adf / Arexx.WB / Misc / Recursion < prev    next >
Text File  |  1992-02-12  |  4KB  |  129 lines

  1. /* Recursion     an interactive demonstration     14-Mar-90 dh
  2.  
  3. An interactive example of recursion.
  4. --------------------------------------------------------------------------- */
  5. OPTIONS RESULTS
  6.  
  7. ClearScreen = d2c(12)
  8. SAY ClearScreen
  9. SAY "This is a short example of recursion, utilizing a Power function.  Below"
  10. SAY "are two versions, one in pseudocode, the other in MathVision syntax.  They"
  11. SAY "both calculate 3 to the fourth power, 3^4. Take a moment to compare them."
  12. SAY ""
  13. SAY "Answer = Power(3,4)                 INVOCATION OF POWER FUNCTION"
  14. SAY "Power(num,pow)                      DEFINITION OF POWER FUNCTION"
  15. SAY " IF pow>0"
  16. SAY "   THEN return num*Power(num,pow-1)"
  17. SAY "   ELSE return 1"
  18. SAY ""
  19. SAY ""
  20. SAY ""
  21. SAY "VAL: fa(3,4)                         MATHVISION INVOCATION OF POWER FUNC"
  22. SAY "FA:  if( a2>0, a1*fa(a1,a2-1), 1 )   MATHVISION DEFINITION OF POWER FUNC" 
  23. ok = WaitForReturn("When done looking, Press <RETURN> ")
  24.  
  25. SAY ClearScreen||"This screen explains the labels on the example."
  26. SAY 
  27. SAY "EXPANSION:      Power Expansion shown in upper lefthand corner"
  28. SAY ""
  29. SAY "NUMBER & POWER: These columns show the values which were passed to the"
  30. SAY "                current invocation of the power function.  They correspond"
  31. SAY "                with A1 and A2 in MathVision."
  32. SAY ""
  33. SAY "CALLS:          When a call is made to the Power function, it is noted here."
  34. SAY ""
  35. SAY "CALCULATION:    Show the calculations performed by this invocation."
  36. SAY ""
  37. SAY "RETURNS:        Show the value returned by this invocation."
  38.  
  39. ok = WaitForReturn("My thinking cap is ON.  Press <RETURN> ")
  40.  
  41. Say ClearScreen
  42. Number = GetAns("Enter a Number",3)
  43. Power = GetAns("Enter a Power", 4)
  44. if (power<0) THEN power = 0
  45. if (power>10) THEN power = 10
  46. ok = WaitForReturn("We are going to calculate "Number"^"Power"  Press <RETURN> ")
  47. Say ClearScreen
  48.  
  49. SAY xy(1,3)||"NUMBER   POWER     CALLS"xy(40,3)"CALCULATIONS"||xy(55,3)"RETURN"
  50. ok = StartPower(Number,Power)
  51.  
  52. EXIT
  53.  
  54. /*------------------------------- StartPower ---------------------------- */
  55. StartPower:
  56.   ARG Number, Power
  57.   LineNum = 4
  58.   
  59.   SAY xy(20,LineNum)||"Power("Number","Power")"
  60.   Answer = DoPower(Number,Power,LineNum+1, 0)
  61.   say xy(52,LineNum)" = "Answer 
  62.   ok = WaitForReturn("The answer is "Answer". Press <RETURN> ")
  63.   Say ClearLine(LineNum)
  64.  
  65.   RETURN Answer
  66.  
  67. /*------------------------------- DoPower ------------------------------- */
  68. DoPower: PROCEDURE
  69.   ARG Number, Power, LineNum, Invocation
  70.  
  71.   say ClearLine(1)
  72.   if (Invocation=0)
  73.     THEN Expansion = xy(1,1)"Expansion: "
  74.     ELSE Expansion = xy(1,1)"Expansion: "||Space(Copies(" "||Number,invocation),1,"*")||"*"
  75.   Say Expansion||Number"^"Power
  76.   say xy(3,LineNum)||Number||xy(12,LineNum)||Power
  77.   IF (Power <= 0)
  78.     THEN
  79.       DO
  80.         val = 1
  81.         say xy(37,LineNum)||" = 1"xy(52,LineNum)" = "Val
  82.         ok = WaitForReturn("Recursion limit reached: Returning 1. Press <RETURN> ")
  83.         Say ClearLine(1)||Expansion||"1"
  84.       END
  85.     ELSE
  86.       DO
  87.         ok = WaitForReturn("Need to evaluate Power("Number","Power-1")  Press <RETURN> ")
  88.         say xy(20,LineNum)||Number"*Power("Number","Power-1")"
  89.         val = DoPower(Number,Power-1,LineNum+1,Invocation+1)
  90.         say xy(37,LineNum)||" = "||Number||"*"Val||xy(52,LineNum)" = "Val*Number 
  91.         val = val * number
  92.         ok = WaitForReturn("Returning "Val".  Press <RETURN> " )
  93.         Say ClearLine(1)||Expansion||Val
  94.       END
  95.   Say ClearLine(LineNum)
  96.   RETURN val
  97.  
  98. /*------------------------------ WaitForReturn -------------------------- */
  99. WaitForReturn:
  100.   PARSE ARG TellMe
  101.   len = Length(TellMe)
  102.   Options Prompt xy(77-len, 20)||TellMe
  103.   PULL response
  104.   say xy(77-len,20)||Copies(" ",len)
  105.   RETURN response
  106.  
  107. /*------------------------------ ClearLine ------------------------------ */
  108. ClearLine:    /* return string which clears a given line */
  109.   Arg line
  110.   Return xy(1,line)||Copies(" ",78)||xy(1,line)
  111.  
  112. /*---------------------------------- XY --------------------------------- */
  113. XY:    /* return string which sets cursor to x,y coordinate */
  114.   ARG x,y
  115.   RETURN d2c(155)||y||";"||x"H"
  116.  
  117. /* --------------------------------- GetAns -------------------------------- */
  118. /* result = GetAns( prompt, default) */
  119. /* prompt for input, displaying default. Accept one item. If only <return> */
  120. /* was pressed, return the default answer */
  121.  
  122. GetAns:
  123.   parse arg prompter, default
  124.   Options Prompt Prompter "(" Default "): "
  125.   pull response
  126.   if (response = "") THEN response = default
  127.   return(response)
  128.  
  129.